package t.com.presenter;

import java.lang.ref.WeakReference;

import t.com.base.BasePresenter;
import t.com.view.BaseView;

/**
 * @author：zcx
 * @date:on 2018/2/28 0028 15:34
 */

public class AppPresenter<V extends BaseView> implements BasePresenter<V> {

    /*
     * View的弱引用
     */
    private WeakReference<V> viewRef;

    /**
     * 关联
     */
    @Override
    public void attachView(V view) {
        viewRef = new WeakReference<V>(view);
    }

    /**
     * 得到VIew
     * @return
     */
   public V getView() {
        return viewRef==null ? null : viewRef.get();
   }

    /**
     * 判断viewRef是否为空
     */
    public boolean isViewAttachd() {
        return viewRef!=null && viewRef.get()!=null;
    }
    /**
     * 解除关联
     */
    @Override
    public void detachView() {
        if(viewRef!=null) {
            viewRef.clear();
            viewRef = null;
        }
    }
}
